package com.song.srb.interal.controller;

import com.song.srb.interal.entity.IntegralGrade;
import com.song.srb.interal.service.IIntegralGradeService;
import com.srb.common.BusinessException;
import com.srb.common.IdWorker;
import com.srb.common.R;
import com.srb.common.ResponseEnum;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author song
 * @since 2024-02-27
 */
@RestController
@RequestMapping("/integral/integralGrade")
public class IntegralGradeController {

    @Resource
    private IIntegralGradeService integralGradeService;

    @DeleteMapping("/{id}")
    @Operation(description = "删除积分等级表", tags = "积分等级表",method = "DELETE")
    public R deleteIntegralGrade(@PathVariable("id") Long id){
        boolean flag = this.integralGradeService.removeById(id);
        return  flag ? R.ok().message("删除成功") : R.error();
    }

    @GetMapping("/list")
    public R listAll(){
            int m = 11 / 0;
        return R.ok().data("list",this.integralGradeService.list());
    }

    @PostMapping("/")
    public R addInteraGrade(@RequestBody IntegralGrade integralGrade){
        boolean save = this.integralGradeService.save(integralGrade);
        return  save ? R.ok().message("添加成功"): R.error();
    }

    @PutMapping("/")
    public R updateInteraGrade(@RequestBody IntegralGrade integralGrade){
        return  this.integralGradeService.saveOrUpdate(integralGrade) ?
                R.ok().message("更新成功") : R.error();
    }


    @PostMapping("/save")
    public R save(@RequestBody IntegralGrade integralGrade){
        //如果借款额度为空就手动抛出一个自定义的异常！
//        if(integralGrade.getBorrowAmount() == null){
//            //BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR.getCode(),
//                     ResponseEnum.BORROW_AMOUNT_NULL_ERROR.getMessage());
//        }

        //使用自定义断言
        com.srb.common.Assert.nullMessage(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        //设置id
        IdWorker idWorker = new IdWorker();
        integralGrade.setId( idWorker.nextId());

        boolean result = this.integralGradeService.save(integralGrade);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }
}
